﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ICSharpCode.AvalonEdit;
using Domain;

namespace Translator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Domain.Translator translator;
        public MainWindow()
        {
            InitializeComponent();
            translator = new Domain.Translator();
        }

        public void OnSemanticsSubmited()
        {
        }
        public void OnTranslateRequested()
        {

        }

        public void OnExecuteRequested()
        {
        }

        private void Submit_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void Translate_Click_1(object sender, RoutedEventArgs e)
        {
            lambdaEditor.Text = translator.Translate(programEditor.Text);
        }

        private void Execute_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                var lambdaText = lambdaEditor.Text;
                var args = Arguments.Text;
                if (String.IsNullOrEmpty(lambdaText) || String.IsNullOrEmpty(args))
                    return;

                var input = new Dictionary<InputStrings, string>();
                input.Add(InputStrings.expresion, lambdaEditor.Text);
                input.Add(InputStrings.args, Arguments.Text);
                var executionResult = translator.Execute(input);

                ExecutionResult.Text = executionResult[OutputStrings.result];
                Logs.Text = executionResult[OutputStrings.log];
            }
            catch (Exception exc)
            {
                StringBuilder result = new StringBuilder();
                using (StreamReader sr = new StreamReader("log.txt")) 
                {
                    result.Append(sr.ReadToEnd());
                }
                Logs.Text = result.ToString();
                File.Delete("log.txt");
                MessageBox.Show("Error while execution");
            }

        }
    }

}
